package com.kunpeng.jvsichuanairspider.utils;

import com.kunpeng.jvsichuanairspider.bll.Shjmpt;
import com.kunpeng.jvsichuanairspider.entity.ShjmptUser;
import com.kunpeng.jvsichuanairspider.entity.VIPInfo;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
/**
 * Created by Administrator on 2017/5/11.
 */
public class DBUtils {
    private final static String resource = "db.xml";
    private static final Logger LOGGER = LoggerFactory.getLogger(DBUtils.class);
    private static final InputStream is = DBUtils.class.getClassLoader().getResourceAsStream(resource);
    private static final SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);

    public int insertVIPInfo(VIPInfo vIPInfo){
        SqlSession  session = sessionFactory.openSession();
        try{
            String statement = "com.kunpeng.jvsichuanairspider.dao.export.ExportMapper.insertVIPInfo";
            int res = session.insert(statement, vIPInfo);
            session.commit();
            return res;
        }catch (Exception ex){
                LOGGER.info("异常：", ex);
                //throw new RuntimeException(ex);
        }finally {
                if(session != null){
                    session.close();
                }

        }
        return 0;

    }

    public ShjmptUser getShjmptUser(ShjmptUser shjmptUser){
        SqlSession session = sessionFactory.openSession();
        try{
            String statement = "com.kunpeng.jvsichuanairspider.dao.export.ExportMapper.getShjmptUser";
            ShjmptUser res = session.selectOne(statement, shjmptUser);
            session.commit();
            return res;
        }catch (Exception ex){
            LOGGER.info("异常：", ex);
        }finally {
            if(session != null){
                session.close();
            }

        }
        return null;
    }

    public int updateToken(ShjmptUser shjmptUser){
        SqlSession session = sessionFactory.openSession();
        try{
            String statement = "com.kunpeng.jvsichuanairspider.dao.export.ExportMapper.updateToken";
            int res = session.update(statement, shjmptUser);
            session.commit();
            return res;
        }catch (Exception ex){
            LOGGER.info("异常：", ex);
        }finally {
            if(session != null){
                session.close();
            }
        }
        return -1;
    }
}
